$(document).ready(function () {
    var moes = $("#moes").kendoDropDownList({
        filter: "contains",
        placeholder: "Elige el MOE...",
        dataTextField: "text",
        dataValueField: "value",
        dataSource: {
            type: "json",
            transport: {
                read: urlMoes
            }
        },
        dataBound:function(){
            if( appBosch.init && appBosch.moeId ){
                moes.value(appBosch.moeId);
                areas.refresh();
                appBosch.moeId = '1';
            }
        }
    }).data("kendoDropDownList");

    var areas = $("#areas").kendoDropDownList({
        autoBind: false,
        cascadeFrom: "moes",
        filter: "contains",
        placeholder: "Elige el Área...",
        dataTextField: "text",
        dataValueField: "value",
        dataSource: {
            serverFiltering: true,
            type: "json",
            transport: {
                read: urlAreas
            }
        },
        dataBound:function(){
            if( appBosch.init && appBosch.moeId === '' && appBosch.areaId ){
                /*var index = 0;
                 while( areas.dataItem(index) !== undefined ){
                 var areaDItem = areas.dataItem(index);
                 if( areaDItem.value === appBosch.areaId ){
                 break;
                 }
                 index++;
                 }
                 areas.select(index);*/
                areas.value(appBosch.areaId);
                appBosch.areaId = '1';
            }else if( appBosch.moeId === '1' ){
                appBosch.moeId = '';
            }
        }
    }).data("kendoDropDownList");

    var lines = $("#lineas").kendoDropDownList({
        autoBind: false,
        cascadeFrom: "areas",
        filter: "contains",
        placeholder: "Elige la Línea...",
        dataTextField: "text",
        dataValueField: "value",
        dataSource: {
            serverFiltering: true,
            type: "json",
            transport: {
                read: urlLines
            }
        },
        dataBound:function(){
            if( appBosch.init && !appBosch.moeId && !appBosch.areaId && appBosch.lineId ){
                lines.value(appBosch.lineId);
                appBosch.lineId = '';
                appBosch.init   = false;
                if( appBosch.nominas !== '' ){
                    $( '#employees' ).val( appBosch.nominas );
                    $( '#btnImprimir' ).click();
                }
            }else if( appBosch.areaId === '1' ){
                appBosch.areaId = '';
            }
        }
    }).data("kendoDropDownList");

    drawSecurity();

    if ( appBosch.left > 0 ) {
        getNext();
    } else {
        $('#waiting').remove();
    }

    $('.print-button span').on('click', function () {
        window.print();
    });
});

function getNext() {
    $.get(appBosch.urlNext, {}, function (resp) {
        if ( resp != '' ) {
            $('#mainContent').append(resp);
            drawSecurity();
            getNext();
        } else {
            $('#waiting').remove();
        }
    });
}

function drawSecurity() {
    var cont = $('#mainContent');
    var divBARcodes = cont.find('.kbarcode');
    var divQRcodes = cont.find('.qrcode');
    for (i=0; i < divBARcodes.length; i++) {
        $(divBARcodes[i]).kendoBarcode({
            value: $(divBARcodes[i]).attr('data-codeforbar'),
            type: "code128",
            width: 300,
            height: 60,
            text: {
                color: "black",
                font: "12px Consolas, Monaco, Sans Mono, monospace, sans-serif !important",
                margin: {
                    left: 15
                }
            }
        });
        $(divQRcodes[i]).kendoQRCode({
            value: $(divQRcodes[i]).attr('data-code'),
            size: 120,
            color: $(divQRcodes[i]).attr('data-color'),
            background: "transparent"
        });
        divBARcodes.removeClass('kbarcode');
        divQRcodes.removeClass('qrcode');
    }
}